package mysql

import (
	"doctor-medicine-system/doctorrpc/doctorrpc"

	"gorm.io/gorm"
)

type Medicine struct {
	gorm.Model
	Name  string `gorm:"type:varchar(30);not null;comment:'药品名称'"`
	Stock int64  `gorm:"type:int(11);not null;comment:'药品库存'"`
	Class string `gorm:"type:varchar(30);not null;comment:'药品分类'"`
}

func (m *Medicine) MedicineAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Medicine) FindMedicineByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(m).Error
}

func (m *Medicine) MedicineUpdate(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).Updates(m).Error
}

func (m *Medicine) MedicineList(db *gorm.DB, lists []*doctorrpc.MedicineList) ([]*doctorrpc.MedicineList, error) {
	err := db.Debug().Model(m).
		Select("medicines.id as medicine_id,name,stock,class").
		Find(&lists).Error
	return lists, err
}
